package no.ntnu.item.ttm4120.dhcp.client;

import java.util.HashSet;
import java.util.Random;
import java.util.TreeMap;

import gmi.GroupProxy;
import no.ntnu.item.ttm4120.dhcp.AddressServer;
import no.ntnu.item.ttm4120.dhcp.AddressServerEGMI;
import no.ntnu.item.ttm4120.dhcp.data.AddressPoolEntity;
import no.ntnu.item.ttm4120.dhcp.exception.AddressPoolException;

public class AddressClient {

    private String groupname = "group05";
    private GroupProxy groupProxy;
    private String address = "localhost";
    private TreeMap<String, AddressPoolEntity> map = new TreeMap<String, AddressPoolEntity>();

    public AddressClient(String name, int port, String mode) {
        /*
         *Getting GroupProxy Object to communicate with Object group through
         * Spread Toolkit. 
         */
        groupProxy = new GroupProxy(this, name, port, groupname, address);
       /*
        * Retrieve a proxy for an object group implementing the Address
        * interface.
        */
        AddressServerEGMI addressServer = (AddressServerEGMI) groupProxy.getServer();
        
        ClientRefreshLease refreshLease = new ClientRefreshLease(this);
        new Thread(refreshLease).start();
        
        if(mode.equalsIgnoreCase("single")){        	
	        try {
				AddressPoolEntity address = addressServer.getNewAddress("Client1", 20000);
				System.out.println(address);
			} catch (AddressPoolException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
        }else{
        	
    	AddressPoolEntity address;
        for (int i = 0; i < 5; i++) {
        	try {
				address = addressServer.getNewAddress("Client" + i, 20000);
				if(address == null) {
					System.err.println("No address returned, skip it ...");
					System.exit(1);
//					continue;
				}
//				System.err.println("got add");
				map.put(address.getClient(), address);
//				System.err.println("put add");
				System.err.println(address);
        	} catch (AddressPoolException e) {
    			e.printStackTrace();
//    		} catch (InterruptedException e) {
//				e.printStackTrace();
			}
		}
        }
    			try {
					Thread.sleep(100000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		System.exit(0);

   	}
    
    public static void main(String[] arg) {
        String connName = null, mode = null;
        int port = 0;
        try {
            for (int i = 0 ; i < arg.length ; i += 2) {
         	   if (arg[i].equals("-c")) {
         		   connName = arg[i+1];
         	   } else if (arg[i].equals("-p")) {
         		   port = Integer.parseInt(arg[i+1]);
         	   } else if (arg[i].equals("-m")) {
         		   mode = arg[i+1];
         	   } else {
         		   usage();
         	   }
            }
        }
        catch (Exception e) {
     	   usage();
        }
                
        new AddressClient(connName, port, mode);                    
    }    

    private static void usage() {
    	System.out.println("Usage Client :: Client -c <clientname> -p <port>");
    	System.exit(1);
    }

    public GroupProxy getGroupProxy() {
		return groupProxy;
	}
    
	public TreeMap<String, AddressPoolEntity> getMap() {
		return map;
	}
}
